package com.xhwl.logistics.converter;

import cn.hutool.core.date.DateUtil;
import com.xhwl.logistics.bo.VehiclePayRecordStatisticsBO;
import com.xhwl.logistics.bo.VehiclePayRecordStatisticsDateBO;
import com.xhwl.logistics.po.VehiclePayRecordStatisticsDatePO;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class ToVehiclePayRecordStatisticsBO {

    public VehiclePayRecordStatisticsBO toVehiclePayRecordStatisticsBO(List<VehiclePayRecordStatisticsDatePO> list, String parkingLotName, Integer type, Date startTime, Date endTime) {
        VehiclePayRecordStatisticsBO vehiclePayRecordStatisticsBO = new VehiclePayRecordStatisticsBO(type, startTime, endTime);

        vehiclePayRecordStatisticsBO.setParkingLotName(parkingLotName);
//        vehiclePayRecordStatisticsBO.setPay(list.stream().map(VehiclePayRecordStatisticsDatePO::toVehiclePayRecordStatisticsDateBO).collect(Collectors.toList()));

        // 按季度划分需要特殊处理下
        if(type.equals(6)) {
            List<VehiclePayRecordStatisticsDateBO> pay = vehiclePayRecordStatisticsBO.getPay();
            for(VehiclePayRecordStatisticsDatePO po : list) {
                // 判断该日期属于第几季度
                Integer quarter = DateUtil.quarter(DateUtil.parseDate(po.getDate()));
                VehiclePayRecordStatisticsDateBO bo = pay.get(quarter - 1);
                bo.setCount(bo.getCount() + po.getCount());
            }
            pay.forEach(item -> item.setCount(item.getCount() / 100));
        } else {
            //这里的赋值逻辑可以优化，目前是使用双重循环
            list.forEach(vehiclePayRecordStatisticsDatePO -> {
                String date = vehiclePayRecordStatisticsDatePO.getDate();
                vehiclePayRecordStatisticsBO.getPay().forEach(vehiclePayRecordStatisticsDateBO -> {
                    if (date.equals(vehiclePayRecordStatisticsDateBO.getDate())) {
                        vehiclePayRecordStatisticsDateBO.setCount(vehiclePayRecordStatisticsDatePO.getCount()/100);
                    }
                });
            });
        }
        return vehiclePayRecordStatisticsBO;
    }

}
